#!/bin/bash
# proxy.sh - 代理切换脚本
# ----------------------------------------------------------------
# 使用方法：
#   source proxy.sh on     # 启用代理
#   source proxy.sh off    # 禁用代理
#   source proxy.sh status # 查看代理状态
#   source proxy.sh        # 显示帮助信息
# ----------------------------------------------------------------

# 代理服务器配置（HTTP/HTTPS 和 SOCKS5 使用同一个服务器）
PROXY_HOST="192.168.208.128"
PROXY_PORT="10808"
PROXY_URL="http://${PROXY_HOST}:${PROXY_PORT}"
SOCKS5_URL="socks5://${PROXY_HOST}:${PROXY_PORT}"

# --- 颜色定义 ---
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# --- 启用代理 ---
proxy_on() {
    export http_proxy="${PROXY_URL}"
    export https_proxy="${PROXY_URL}"
    export HTTP_PROXY="${PROXY_URL}"
    export HTTPS_PROXY="${PROXY_URL}"
    export ftp_proxy="${PROXY_URL}"
    export FTP_PROXY="${PROXY_URL}"
    export all_proxy="${SOCKS5_URL}"
    export ALL_PROXY="${SOCKS5_URL}"
    export no_proxy="localhost,127.0.0.1,::1"
    export NO_PROXY="localhost,127.0.0.1,::1"
    
    echo -e "${GREEN}✓ 代理已启用${NC}"
    echo -e "${BLUE}HTTP/HTTPS Proxy: ${PROXY_URL}${NC}"
    echo -e "${BLUE}SOCKS5 Proxy:     ${SOCKS5_URL}${NC}"
}

# --- 禁用代理 ---
proxy_off() {
    unset http_proxy
    unset https_proxy
    unset HTTP_PROXY
    unset HTTPS_PROXY
    unset ftp_proxy
    unset FTP_PROXY
    unset all_proxy
    unset ALL_PROXY
    unset no_proxy
    unset NO_PROXY
    
    echo -e "${YELLOW}✓ 代理已禁用${NC}"
}

# --- 查看代理状态 ---
proxy_status() {
    echo -e "${BLUE}=== 当前代理状态 ===${NC}"
    if [ -n "$http_proxy" ] || [ -n "$HTTP_PROXY" ] || [ -n "$all_proxy" ]; then
        echo -e "${GREEN}代理：已启用${NC}"
        echo "http_proxy:  ${http_proxy:-未设置}"
        echo "https_proxy: ${https_proxy:-未设置}"
        echo "all_proxy:   ${all_proxy:-未设置}"
        echo "no_proxy:    ${no_proxy:-未设置}"
    else
        echo -e "${YELLOW}代理：未启用${NC}"
    fi
}

# --- 测试代理连接 ---
proxy_test() {
    echo -e "${BLUE}=== 测试代理连接 ===${NC}"
    echo "测试代理服务器: ${PROXY_URL}"
    
    # 测试代理是否可达
    if command -v nc &> /dev/null; then
        if nc -z -w3 "${PROXY_HOST}" "${PROXY_PORT}" 2>/dev/null; then
            echo -e "${GREEN}✓ 代理服务器可达${NC}"
        else
            echo -e "${RED}✗ 代理服务器无法连接${NC}"
            return 1
        fi
    else
        echo -e "${YELLOW}⚠ 未安装 nc 命令，跳过连接测试${NC}"
    fi
    
    # 测试网络访问（如果代理已启用）
    if [ -n "$http_proxy" ]; then
        echo "测试网络访问 (google.com)..."
        if curl -I --connect-timeout 5 -x "${PROXY_URL}" https://www.google.com &>/dev/null; then
            echo -e "${GREEN}✓ 通过代理访问网络成功${NC}"
        else
            echo -e "${RED}✗ 通过代理访问网络失败${NC}"
        fi
    fi
}

# --- 显示帮助信息 ---
proxy_help() {
    echo -e "${BLUE}=== 代理切换脚本使用说明 ===${NC}"
    echo ""
    echo "使用方法："
    echo "  source proxy.sh on       # 启用代理"
    echo "  source proxy.sh off      # 禁用代理"
    echo "  source proxy.sh status   # 查看代理状态"
    echo "  source proxy.sh test     # 测试代理连接"
    echo "  source proxy.sh help     # 显示帮助信息"
    echo ""
    echo "简化命令（建议添加到 ~/.bashrc 或 ~/.zshrc）："
    echo "  alias pon='source $(pwd)/proxy.sh on'"
    echo "  alias poff='source $(pwd)/proxy.sh off'"
    echo "  alias pst='source $(pwd)/proxy.sh status'"
    echo ""
    echo "当前代理配置："
    echo "  服务器: ${PROXY_HOST}"
    echo "  端口:   ${PROXY_PORT}"
    echo "  HTTP/HTTPS: ${PROXY_URL}"
    echo "  SOCKS5:     ${SOCKS5_URL}"
    echo ""
    echo -e "${YELLOW}注意：必须使用 'source' 命令，否则环境变量不会生效！${NC}"
}

# --- 主逻辑 ---
case "$1" in
    on|enable|start)
        proxy_on
        ;;
    off|disable|stop)
        proxy_off
        ;;
    status|st|show)
        proxy_status
        ;;
    test|check)
        proxy_test
        ;;
    help|h|-h|--help)
        proxy_help
        ;;
    "")
        # 无参数时显示帮助
        proxy_help
        ;;
    *)
        echo -e "${RED}错误：未知参数 '$1'${NC}"
        echo "使用 'source proxy.sh help' 查看帮助"
        ;;
esac

